<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="产品编号" prop="productNumber">
        <el-input
          v-model="queryParams.productNumber"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="部件编号" prop="partNumber">
        <el-input
          v-model="queryParams.partNumber"
          placeholder="请输入部件编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="部件名称" prop="deviceOrPartName">
        <el-input
          v-model="queryParams.deviceOrPartName"
          placeholder="请输入部件名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="图纸图号" prop="drawingNumber">
        <el-input
          v-model="queryParams.drawingNumber"
          placeholder="请输入图纸图号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div style="width: 100%; height: 50px; display: flex; justify-content: space-between">
      <div>
        <el-button
          type="primary"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['order:abnormal-materials:export']"
        >
          数据导出
        </el-button>
      </div>
      <el-button type="primary" @click="openTableTitleSet"> 表头设置 </el-button>
    </div>
    <el-table
    ref="multipleTableRef"
      border
      row-key="id"
      :height="tableHeight"
      :tree-props="{ children: 'children' }"
      default-expand-all
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        align="center"
        :selectable="isSelected"
        :reserve-selection="true"
        type="selection"
        fixed="left"
      />
      <el-table-column label="序号" align="center" prop="parentIndex" type="" fixed="left" />
      <el-table-column
        v-if="tableTitleList.length"
        :min-width="tableTitleList[1].width"
        :header-align="tableTitleList[1].headerAlign"
        :align="tableTitleList[1].align"
        :label="tableTitleList[1].label"
        :prop="tableTitleList[1].field"
      >
        <template #default="scope">
          <dict-tag
            v-if="tableTitleList[1].dictType"
            :type="tableTitleList[1].dictType"
            :value="scope.row[tableTitleList[1].field]"
          />
          <span v-else>{{ scope.row[tableTitleList[1].field] }}</span>
        </template>
      </el-table-column>
      <template v-for="(item, index) in tableTitleList">
        <el-table-column
          v-if="item.isTable && ![0, 1].includes(index)"
          :key="index"
          :type="item.type"
          :min-width="item.width"
          :header-align="item.headerAlign"
          :align="item.align"
          :label="item.label"
          :prop="item.field"
        >
          <template #default="scope">
            <dict-tag v-if="item.dictType" :type="item.dictType" :value="scope.row[item.field]" />
            <template v-else>
              <el-button
                v-if="item.label == '图纸图号'"
                type="primary"
                link
                @click="jumpPdfFile(scope.row[item.field])"
                >{{ scope.row[item.field] }}</el-button
              >
              <template v-else>{{ scope.row[item.field] }}</template>
            </template>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
  <!-- 表头设置 -->
  <TableWidthSet ref="tableTitleSet" @refresh="getLabelAll" />
</template>

<script setup lang="ts" name="OrderAbnormalMaterials">
   import download from '@/utils/download'
   import { jumpPdfFile } from '@/utils/formCreate'
   import * as AbnormalMaterialsApi from '@/api/produce/workOrderManagement/abnormalMaterials'

   const loading = ref(true) // 列表的加载中
   const total = ref(0) // 列表的总页数
   const list = ref([]) // 列表的数据
   const queryParams = reactive({
     pageNo: 1,
     pageSize: 50,
     productNumber: null,
     productName: null,
     partNumber: null,
     deviceOrPartName: null,
     drawingNumber: null
   })
   const queryFormRef = ref() // 搜索的表单
   const exportLoading = ref(false) // 导出的加载中
   let tableTitleList = ref<CrudSchema[]>([]) // 表头的数据
   /** 查询列表 */
   const getList = async () => {
     loading.value = true
     try {
       const data = await AbnormalMaterialsApi.getAbnormalMaterialsPage(queryParams)
       list.value = data.list.map((item, index) => {
         item.parentIndex = index + 1 //父元素序号
         if (item.children.length) {
           item.children.map((child, childIndex) => {
             child.index = childIndex //子类编辑时的下标
             child.parent = index //父类下标
             return child
           })
         }
         return item
       })
       tableTitleList.value = data.labels
       total.value = data.total
     } finally {
       loading.value = false
     }
   }
   /**选中 */
   const selectList = ref([])
   const handleSelectionChange = (data) => {
     selectList.value = data
   }
   /** 搜索按钮操作 */
   const handleQuery = () => {
     queryParams.pageNo = 1
     getList()
   }

   /** 重置按钮操作 */
   const resetQuery = () => {
     queryFormRef.value.resetFields()
     handleQuery()
   }

   /** 导出按钮操作 */
   const exportRef = ref()
   const handleExport = () => {
     exportRef.value.open()
   }
   const multipleTableRef = ref()
   // 导出
   const downloadFun = async () => {
     const val = exportRef.value.params
     let params = {
       fileName: val.name,
       ids: ''
     }
     if (val.range === 'all') {
       params.ids = ''
     } else {
       params.ids = selectList.value.map((item) => item.id)
     }
     
     AbnormalMaterialsApi.exportAbnormalMaterials(params).then((res) => {
       download.excel(res, val.name)
       clearSelectData()
     })
   }

   //清空多选
   const clearSelectData = () => {
     selectList.value = []
     multipleTableRef.value!.clearSelection()
   }
   /**打开表格表头设置**/
   const tableTitleSet = ref()
   const openTableTitleSet = () => {
     let code = 'abnormalMaterials'
     tableTitleSet.value.open(tableTitleList.value, code)
   }

   /** 初始化 **/
   const tableHeight = ref(0)
   //132：表格内除表格以外的高度，127 顶部固定高度 + copyright高度
   onMounted(() => {
     const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
     tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
     getList()
   })
</script>
